package com.ungao.framework.parameter;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ParameterHandler {
    
    private PreparedStatement preparedStatement;

    public ParameterHandler(PreparedStatement preparedStatement) {
        this.preparedStatement = preparedStatement;
    }

    public void setParameters(Object[] parameters) {
        try {
            if (null != parameters) {
                for (int i = 0; i < parameters.length; i++) {
                    if (parameters[i] instanceof Integer) {
                        this.preparedStatement.setInt(i + 1, (Integer) parameters[i]);
                    } else if (parameters[i] instanceof Long) {
                        this.preparedStatement.setLong(i + 1, (Long) parameters[i]);
                    } else if (parameters[i] instanceof String) {
                        this.preparedStatement.setString(i + 1, String.valueOf(parameters[i]));
                    } else if (parameters[i] instanceof Boolean) {
                        this.preparedStatement.setBoolean(i + 1, (Boolean) parameters[i]);
                    } else {
                        this.preparedStatement.setString(i + 1, String.valueOf(parameters[i]));
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
